/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import BLL.BLLNguoiDung;
import DTO.DTONguoiDung;
import javax.swing.JOptionPane;

/**
 *
 * @author Documents
 */
public class SuaNguoiDung extends javax.swing.JDialog {
    BLLNguoiDung bllNgDung = new BLLNguoiDung();
    String tendn;
    /**
     * Creates new form SuaNguoiDung
     * @param tendn
     */
    public SuaNguoiDung(String tendn) {
        initComponents();
        LayDLVaoBang(tendn);
        this.tendn = tendn;
    }


    private void LayDLVaoBang(String tendn){
        DTONguoiDung dtoNgDung = bllNgDung.LayNgDungTheoTen(tendn);
        if(dtoNgDung != null){
            jtxtDangNhap.setText(dtoNgDung.getTenDN());
            jtxtEmail.setText(dtoNgDung.getEmail());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jtxtDangNhap = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jtxtMK = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jtxtNhacMk = new javax.swing.JPasswordField();
        jLabel16 = new javax.swing.JLabel();
        jtxtEmail = new javax.swing.JTextField();
        jbtnLuu = new javax.swing.JButton();
        jbtnHuy = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 0));
        jLabel2.setText("SỬA TÀI KHOẢN");

        jLabel15.setText("Tên đăng nhập");

        jtxtDangNhap.setEnabled(false);
        jtxtDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtDangNhapActionPerformed(evt);
            }
        });

        jLabel1.setText("Mật khẩu");

        jLabel6.setText("Nhập lại mật khẩu");

        jLabel16.setText("Email");

        jbtnLuu.setText("Lưu");
        jbtnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLuuActionPerformed(evt);
            }
        });

        jbtnHuy.setText("Đóng");
        jbtnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(82, 82, 82)
                            .addComponent(jLabel2))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel15)
                                .addComponent(jLabel1)
                                .addComponent(jLabel6))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtxtNhacMk)
                                .addComponent(jtxtMK)
                                .addComponent(jtxtDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(jbtnLuu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnHuy)
                        .addGap(17, 17, 17))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jtxtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jtxtDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtMK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNhacMk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnLuu)
                    .addComponent(jbtnHuy))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtDangNhapActionPerformed
        
    }//GEN-LAST:event_jtxtDangNhapActionPerformed

    private void jbtnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHuyActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtnHuyActionPerformed

    private void jbtnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLuuActionPerformed
        try {
            if("".equals(KiemTra()))
            {
                String MK= jtxtMK.getText();
                String NhacMK = jtxtNhacMk.getText();
                String email = jtxtEmail.getText();
                
                DTONguoiDung dtoNgDung = new DTONguoiDung();
                dtoNgDung.setTenDN(this.tendn);
                dtoNgDung.setMatKhau(MK);
                dtoNgDung.setEmail(email);
                if(bllNgDung.SuaNguoiDung(dtoNgDung))
                {
                    JOptionPane.showMessageDialog(null, "Sửa tài khoản thành công");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Không thể sửa");
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null, KiemTra());
            }
        } 
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Lỗi, kiểm tra lại");
        }
    }//GEN-LAST:event_jbtnLuuActionPerformed

    private String KiemTra(){
        String kq = "";
//        DTONguoiDung dtoNgDung = new DTONguoiDung();
        if("".equals(jtxtMK.getText().trim()))
            kq += "Vui lòng nhập mật khẩu\n";
//        if(!dtoNgDung.getMatKhau().equals(jtxtMK.getText()))
//            kq += "Sai mật khẩu";
        if("".equals(jtxtNhacMk.getText().trim()))
            kq += "Vui lòng nhập lại mật khẩu\n";
        if(!jtxtNhacMk.getText().trim().equals(jtxtMK.getText().trim()))
            kq += "Mật khẩu nhắc lại không đúng\n";
        String emailreg = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        if(!jtxtEmail.getText().trim().matches(emailreg))
            kq += "Địa chỉ mail không hợp lệ";
        return kq;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SuaNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SuaNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SuaNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SuaNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new SuaNguoiDung().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbtnHuy;
    private javax.swing.JButton jbtnLuu;
    private javax.swing.JTextField jtxtDangNhap;
    private javax.swing.JTextField jtxtEmail;
    private javax.swing.JPasswordField jtxtMK;
    private javax.swing.JPasswordField jtxtNhacMk;
    // End of variables declaration//GEN-END:variables
}
